/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webproject.jsp.common;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;

/**
 *
 * @author jeong
 */
public class ImageUtil {
    
    public static final int SAME = -1;
    public static final int RATIO = 0;
    
    /**
     * 
     * @param src
     * @param dest
     * @param width
     * @param height
     * @throws IOException 
     */
    public static void resize(File src, File dest, int width, int height) throws IOException {
        
        FileInputStream srcIs = null;
        
        try {
            srcIs = new FileInputStream(src);
            ImageUtil.resize(src, dest, width, height);
        } finally {
            if (srcIs != null)
                try {
                    srcIs.close();
                } catch(IOException ioe) {}
        }
    }
    
    /**
     * 
     * @param src
     * @param dest
     * @param width
     * @param height
     * @throws IOException 
     */
    public static void resize(InputStream src, File dest, int width, int height) throws IOException {
        
        BufferedImage srcImg = ImageIO.read(src);
        int srcWidth = srcImg.getWidth();
        int srcHeight = srcImg.getHeight();
        
        int destWidth = -1, destHeight = -1;
        
        if (width == SAME) {
            destWidth = srcWidth;
        } else if(width > 0) {
            destWidth = width;
        }
        
        if (height == SAME) {
            destHeight = srcHeight;
        } else if(height > 0) {
            destHeight = height;
        }
        
        if (width == RATIO && height == RATIO) {
            destWidth = srcWidth;
            destHeight = srcHeight;
        } else if (width == RATIO) {
            double ratio = (double)destHeight / ((double)srcHeight);
            destWidth = (int) ((double)srcWidth * ratio);
        } else if (height == RATIO) {
            double ratio = ((double)destWidth / ((double)srcWidth));
            destHeight = (int)((double)srcHeight * ratio);
        }
        
        BufferedImage destImg = new BufferedImage(
                destWidth, destHeight, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = destImg.createGraphics();
        g.drawImage(srcImg, 0, 0, destWidth, destHeight, null);
        
        ImageIO.write(destImg, "jpg", dest);
        
        
    }
    
}
